package top.hudk.tank.collider;

import top.hudk.tank.enums.Group;
import top.hudk.tank.factory.Bullet;
import top.hudk.tank.factory.Tank;
import top.hudk.tank.model.GameObject;

import java.awt.*;

/**
 * 作用：
 *
 * @author hudk
 * @date 2020/9/10 23:15
 */
public class BulletTankCollider implements Collider {
    @Override
    public boolean collision(GameObject o1, GameObject o2) {
        Bullet bullet = null;
        Tank tank = null;
        if (o1 instanceof Bullet && o2 instanceof Tank) {
            bullet = (Bullet) o1;
            tank = (Tank) o2;
        } else if (o1 instanceof Tank && o2 instanceof Bullet) {
            bullet = (Bullet) o2;
            tank = (Tank) o1;
        } else {
            return true;
        }
        Rectangle bulletRectangle = bullet.getRect();
        if(bulletRectangle == null){
            System.out.println("bulletRectangle is null");
        }
        Rectangle tankRectangle = tank.getRect();
        if(tankRectangle == null){
            System.out.println("tankRectangle is null");
        }
        Group tankGroup = tank.getGroup();
        if(tankGroup == null){
            System.out.println("tankGroup is null");
        }
        Group bulletGroup = bullet.getGroup();
        if(bulletGroup == null){
            System.out.println("bulletGroup is null");
        }
        if (bullet.getRect().intersects(tank.getRect()) && !tank.getGroup().equals(bullet.getGroup())) {
            tank.hit();
            bullet.die();
            return false;
        }
        return true;
    }
}
